<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
/**
 */
namespace Admin\Controller;
use Think\Controller;
class PublicController extends AdminbaseController {

    function _initialize() {}
    
    //后台登陆界面
    public function login() {
    	if(isset($_SESSION['ADMIN_ID'])){//已经登录
    		$this->success(L('LOGIN_SUCCESS'),U("Admin/Index/index"));
    	}else{
    		$this->display("./login");
    	}
    }
    
    public function logout(){
    	session('ADMIN_ID',null); 
    	$this->redirect("public/login");
    }
    
    public function dologin(){
    	$name = I("post.username");
    	if(empty($name)){
    		$this->error('用户名不能为空');
    	}
    	$pass = I("post.password");
    	if(empty($pass)){
    		$this->error('密码不能为空');
    	}
    	$verrify = I("post.verify");
    	if(empty($verrify)){
    		$this->error('验证码不能为空');
    	}
    	//验证码
    	if(!sp_check_verify_code()){
    		$this->error('验证码错误');
    	}else{

    		$user = D("admin");
    		
            $where['name']=$name;
    		
    		$result = $user->where($where)->find();

    		if(!empty($result)){
    			if($result['password'] == md5($pass)){
    				
    				$role_user_model=D("AdminRoleUser");
    				
    				$role_user_join = C('DB_PREFIX').'role as b on a.role_id =b.id';

    				$groups=$role_user_model->alias("a")->join($role_user_join)->where(array("user_id"=>$result["id"],"status"=>1))->getField("role_id",true);

    				$role = $role_user_model->where('user_id='.$result['user_id'].' and role_id=1')->find();
                                
    				//登入成功页面跳转
    				$_SESSION["ADMIN_ID"]=$result["user_id"];
    				$_SESSION['name']=$result["name"];
                                
    				setcookie("admin_username",$name,time()+100*30*24*3600,"/");
    				$this->success('登陆成功',U("Index/index"));
    			}else{
    				$this->error('密码错误');
    			}
    		}else{
    			$this->error('用户不存在');
    		}
    	}
    }

}